package com.doupi.partner.rpc.biz;

import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.doupi.partner.constant.BusinessStatusEnum;
import com.doupi.partner.manager.BusinessRelationManager;
import com.doupi.partner.model.BusinessRelation;
import com.doupi.partner.model.PartnerRelation;
import com.doupi.partner.service.BusinessRelationService;
import com.doupi.partner.service.PartnerRelationService;
import com.doupi.partner.util.AssertUtils;

/**
 * 商家关系
 *
 * @author kxj
 * @version @ 2017-04-17 22:07
 **/
@Component("businessRelationBiz")
public class BusinessRelationBizImpl implements BusinessRelationBiz {
	private final static Logger LOGGER = LoggerFactory.getLogger(BusinessRelationBizImpl.class);
	private final PartnerRelationService partnerRelationService;
	private final BusinessRelationService businessRelationService;
	private final BusinessRelationManager businessRelationManager;

	@Autowired
	public BusinessRelationBizImpl(PartnerRelationService partnerRelationService, BusinessRelationService businessRelationService, BusinessRelationManager businessRelationManager) {
		this.partnerRelationService = partnerRelationService;
		this.businessRelationService = businessRelationService;
		this.businessRelationManager = businessRelationManager;
	}

	@Override
	public int addBusinessRelation(String mobile, String name, String marketNumber, BusinessRelation businessRelation) {
		LOGGER.info("[partner-rpc] - 接到商家信息：商家关系:{}, 商家名称：{}, 手机号：{}, 合伙人营销号：{}", businessRelation, name, mobile, marketNumber);
		AssertUtils.assertNotNull(mobile, "链接不正确");
		AssertUtils.assertNotNull(name, "链接不正确");
		AssertUtils.assertNotNull(marketNumber, "链接不正确");
		AssertUtils.assertNotNull(businessRelation, "链接不正确");
		PartnerRelation partnerRelation = partnerRelationService.selectByMarketNumber(marketNumber);
		AssertUtils.assertNotNull(partnerRelation, "链接不正确");
		businessRelation.setStatus(BusinessStatusEnum.REPORT.getCode());
		businessRelation.setCity(partnerRelation.getCity());
		// TODO 提取
		businessRelation.setMarketingNumber("2" + RandomStringUtils.random(9, "1234567890"));
		businessRelation.setPartnerAdministratorId(partnerRelation.getAdministratorId());
		businessRelation.setSuperiorPartnerAdministratorId(partnerRelation.getSuperiorAdministratorId());
		businessRelation.setCityAdministratorId(partnerRelation.getCityAdministratorId());
		businessRelation.setSubstationAdministratorId(partnerRelation.getSubstationAdministratorId());
		return businessRelationManager.saveBusinessRelation(mobile, name, businessRelation, "123456");
	}

	@Override
	public BusinessRelation getBusinessRelationByBusinessId(String businessId) {
		return businessRelationService.selectBusinessRelationByBusinessId(businessId);
	}

}
